package com.mall.service.authority.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsResource;
import com.mall.model.vo.PageResult;
import com.mall.service.authority.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 查询所有后台资源
     * @return
     */
    @GetMapping("/listAll")
    public CommonResult listAll() {
        List<UmsResource> list = umsResourceService.list();
        return CommonResult.success(list, "查找成功");
    }

    /**
     * 分页模糊查询后台资源
     * @param categoryId
     * @param nameKeyword
     * @param pageNum
     * @param pageSize
     * @param urlKeyword
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(Long categoryId, String nameKeyword, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, String urlKeyword) {

        Page<UmsResource> pageCondition = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(categoryId), UmsResource::getCategoryId, categoryId);
        lqw.like(ObjectUtil.isNotNull(nameKeyword), UmsResource::getName, nameKeyword);
        lqw.like(ObjectUtil.isNotNull(urlKeyword), UmsResource::getUrl, urlKeyword);

        Page pageR = umsResourceService.page(pageCondition, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageR.getRecords());
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotalPage(Convert.toInt(pageR.getPages()));
        pageResult.setTotal(Convert.toInt(pageR.getTotal()));

        return CommonResult.success(pageResult, "查询成功");
    }

    /**
     *添加后台资源
     * @param umsResource
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody UmsResource umsResource){
        Date date=new Date();
        umsResource.setCreateTime(date);
        umsResourceService.save(umsResource);
        return CommonResult.success("添加成功!");
    }

    /**
     * 根据id删除后台资源
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id){
        umsResourceService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 修改后台资源
     * @param umsResource
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@RequestBody UmsResource umsResource,@PathVariable Long id){
        umsResource.setId(id);
        Date date=new Date();
        umsResource.setCreateTime(date);
        umsResourceService.updateById(umsResource);
        return CommonResult.success("修改成功!");
    }

    /**
     * 根据ID获取资源详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable Long id) {
        UmsResource umsResource = umsResourceService.getById(id);
        return CommonResult.success(umsResource);
    }
}
